﻿using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    ///  WebBrowser 帮助类
    /// </summary>
    public static class WebBrowserHelper
    {
        #region WebBrowser添加cookie
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string urlName, string cookieName, string cookieData);
        /// <summary>
        /// WebBrowser添加cookie
        /// </summary>
        /// <param name="webBrowser">WebBrowser</param>
        /// <param name="url">url</param>
        /// <param name="cookie">cookie</param>
        public static void InsertCookie(this WebBrowser webBrowser, string url, Cookie cookie)
        {
            InternetSetCookie(url, cookie.Name, cookie.Value);
        }
        #endregion
        #region 从WebBrowser中获取CookieContainer
        /// <summary>
        /// 从WebBrowser中获取CookieContainer
        /// </summary>
        /// <param name="webBrowser">WebBrowser对象</param>
        /// <returns>CookieContainer</returns>
        public static CookieContainer GetCookieContainer(this WebBrowser webBrowser)
        {
            CookieContainer _cookieContainer = new CookieContainer();

            string _cookieString = webBrowser.Document.Cookie;
            if (string.IsNullOrEmpty(_cookieString)) return _cookieContainer;

            string[] _cookies = _cookieString.Split(';');
            if (_cookies == null) return _cookieContainer;

            foreach (string cookieString in _cookies)
            {
                string[] _cookieNameValue = cookieString.Split('=');
                if (_cookieNameValue.Length != 2) continue;
                Cookie _cookie = new Cookie(_cookieNameValue[0].Trim().ToString(), _cookieNameValue[1].Trim().ToString());
                _cookieContainer.Add(_cookie);
            }
            return _cookieContainer;
        }
        #endregion 
    }
}
